import pandas as pd
import pyarrow.parquet as pq
import os

def analyze_parquet_file(file_path):
    """分析并读取Parquet文件"""
    print(f"分析文件: {file_path}")
    
    # 检查文件大小
    file_size = os.path.getsize(file_path) / (1024 * 1024)  # MB
    print(f"文件大小: {file_size:.2f} MB")
    
    # 使用pyarrow获取元数据
    try:
        parquet_file = pq.ParquetFile(file_path)
        print(f"行组数: {parquet_file.num_row_groups}")
        print(f"总行数: {parquet_file.metadata.num_rows}")
        print(f"列数: {parquet_file.metadata.num_columns}")
        
        # 读取前100行
        df = parquet_file.read_row_group(0).to_pandas().head(100)
        print("\n前100行数据示例:")
        print(df)
        
        return df
    except Exception as e:
        print(f"读取文件出错: {str(e)}")
        return None

if __name__ == "__main__":
    # 替换为您的Parquet文件路径
    parquet_path = "/opt/soft/paimon/default.db/test/bucket-0/data-b79ad303-c412-4478-b278-357b549f28b8-1.parquet"
    
    # 分析并读取文件
    data = analyze_parquet_file(parquet_path)
    
    if data is not None:
        print("\n数据分析摘要:")
        print(f"数据类型:\n{data.dtypes}")
        print(f"数值列统计:\n{data.describe()}")